package com.hichao.cs.thrift.chat;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

import com.hichao.cs.helper.CSHelper;
import com.hichao.cs.thrift.chat.ChatService.Client;

public class ChatClient extends Client implements CSHelper {

	ChatClient(TProtocol prot) {
		super(prot);
	}
	
	public static ChatClient getClient(String host, int port) {
		TSocket socket = new TSocket(host, port);
		TTransport transport = new TFramedTransport(socket);
		TProtocol protocol = new TBinaryProtocol(transport);
		return new ChatClient(protocol);
	}
	
	public void open(){
		TTransport inputTransport = this.getInputProtocol().getTransport();
		if (inputTransport != null && !inputTransport.isOpen())
			try{
				inputTransport.open();
			}catch(Exception x){
				x.printStackTrace();
			}
		TTransport outputTransport = this.getOutputProtocol().getTransport();
		if (outputTransport != null && !outputTransport.isOpen())
			try{
				outputTransport.open();
			}catch(Exception x){
				x.printStackTrace();
			}
	}
	
	public void close(){
		TTransport inputTransport = this.getInputProtocol().getTransport();
		if (inputTransport != null && inputTransport.isOpen())
			try{
				inputTransport.close();
			}catch(Exception x){
				x.printStackTrace();
			}
		TTransport outputTransport = this.getOutputProtocol().getTransport();
		if (outputTransport != null && outputTransport.isOpen())
			try{
				outputTransport.close();
			}catch(Exception x){
				x.printStackTrace();
			}
	}
}
